<?php

Loader::using(array(
    "ext::libs::phpUI::UIControl",
    "ext::libs::phpUI::factories::BaseUIControlFactory",
    "ext::libs::inscripcionesGenericas::filters::*",
    "models::entities::CamposFormularios",
    "ext::libs::phpUI::impl::jquery::constraints::*",
    "ext::libs::phpUI::impl::jquery::controls::*",
));

class QuestionBasedUIControlFactory extends BaseUIControlFactory {

    private $question;
    /**
     *
     * @var QuestionBasedUIControlFactory
     */
    private static $defaultInstance;

    function __construct() {
        parent::setFilters(array(
                    new BasicUIControlFilter(),
                    new BasicDataSetUIControlFilter(),
                    new DataSourceDataSetUIControlFilter(),
                    new ConstraintUIControlFilter(),
                    new DependencyUIControlFilter(),
                ));
    }

    protected function makeInstance() {
        QuestionBasedUIControlFilter::setQuestion($this->question);

        $type = ucwords(strtolower($this->question->getTipo()));
        $control = null;
        if (class_exists($type) && array_search("UIControl", class_parents($type))) {
            $control = new $type();
            $this->setBasicInfo($control);
        }

        return $control;
    }

    protected function setBasicInfo(UIControl &$control) {
        $control->setId($this->question->getId());
        $control->setName($this->question->getId());
        $control->setRequired($this->question->getRequired());
    }

    public function getQuestion() {
        return $this->question;
    }

    public function setQuestion(CamposFormularios $question) {
        $this->question = $question;
    }

    /**
     *
     * @return QuestionBasedUIControlFactory
     */
    public static function instance() {
        if (self::$defaultInstance == NULL) {
            self::$defaultInstance = new QuestionBasedUIControlFactory();
        }

        return self::$defaultInstance;
    }

}

?>
